package com.lznAdmin.system.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.system.oshi.CpuInfo;
import cn.hutool.system.oshi.OshiUtil;
import com.lznAdmin.base.lznAdminResult;
import com.lznAdmin.system.service.SysMonitoringService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import oshi.software.os.OSFileStore;

import java.util.List;

/**
 * @program: lznAdmin-pro
 * @description: 系统监控页
 * @author: lzn
 * @create: 2021-10-26 14:36
 **/
@RestController
public class SysMonitoringController extends BaseController {

    @Autowired
    SysMonitoringService sysMonitoringService;

    @PreAuthorize("hasAuthority('sys:monitoring:list')")
    @GetMapping("/sys/cpuinfo")
    public lznAdminResult getCPUInfo() {
        CpuInfo cpuInfo = OshiUtil.getCpuInfo();
        return lznAdminResult.ok(cpuInfo);
    }

    @PreAuthorize("hasAuthority('sys:monitoring:list')")
    @GetMapping("/sys/memoryinfo")
    public lznAdminResult memoryAndJvmInfo() {
        Dict memoryInfo = sysMonitoringService.getMemoryInfo();
        Dict jvmInfo = sysMonitoringService.getJVMInfo();
        return lznAdminResult.ok(Dict.create().set("memoryInfo", memoryInfo).set("JVMInfo", jvmInfo));
    }

    @PreAuthorize("hasAuthority('sys:monitoring:list')")
    @GetMapping("/sys/javainfo")
    public lznAdminResult getJavaInfo() {
        Dict javaInfo = sysMonitoringService.getJavaInfo();
        List<OSFileStore> diskInfo = sysMonitoringService.getDiskInfo();
        Dict serverInfo = sysMonitoringService.getServerInfo();
        return lznAdminResult.ok(Dict.create().set("javaInfo", javaInfo)
                .set("serverInfo", serverInfo)
                .set("diskInfo", diskInfo));
    }
}
